<?php
namespace app\index\controller;

use app\common\model\Agency;
use app\common\model\Brand;
use app\common\model\Cars;
use app\common\model\CarsInfo;
use app\common\model\OffAbnormal;
use app\common\model\OffNormal;
use app\common\model\Repair;
use app\common\utils\ResultVOUtil;
use think\Db;

class Index extends Base
{
    
    public function cars()
    {
        $brand = Cars::column('brand_id');

        $model = Brand::withCount(['cars'])->select($brand);

        $array = [];
        foreach ($model as $key => $value) {
            $array[$key]['value'] = $value->cars_count;            
            $array[$key]['name'] = $value->name;
        }
        $data = [];
        $sortArray = $this->array_sort($array,'value',SORT_DESC);
        $data['brand'] = $sortArray;
        $data['name'] = array_column($sortArray,'name');
        return ResultVOUtil::success($data);
        
    }
    
    public function agency(){
        $agency_id = session('admin.agency_id');
        if (strpos($agency_id, '1') === false) {
            $model = Agency::withCount(['cars'])->select($agency_id);
        } else{
            $agency = Agency::where('id','>',1)->column('id');
            $model = Agency::withCount(['cars'])->select($agency);
        }

        $array = [];
        foreach ($model as $key => $value) {
            $array['agency'][$key]['name'] = $value->name;
            $array['agency'][$key]['value'] = $value->cars_count;
            $array['address'][$value->name][] = $value->lng;
            $array['address'][$value->name][] = $value->lat;
        }
        return ResultVOUtil::success($array);
    }
    
    public function offLine(){
        $data = Db::name('cars_info')->where(['network_status'=>0])->where('machine_report_time','>',0)
                        ->order('machine_report_time asc')->column('car_unique_id');
        $cars = [];
        $x = 0;
        while ( count($cars)<=10 && $x<count($data)) {
            $dataum = Cars::with('carsInfo')->where(['car_unique_id'=>$data[$x]])->field('car_sn,car_unique_id,brand_id,agency_id,supplier_no')->find();
            $x++;
            if($dataum){
                $cars[] = $dataum;
            }
            
        }
        $offData = [];
        $array = [];
        if($cars){
            foreach ($cars as $key => $value) {
                $time = time()*1000 - ($value['cars_info']['machine_report_time']);
                $array[$key]['value'] = (floor($time/1000/86400));
                $array[$key]['name'] = $value['car_sn'];
            }
            $offData['cars'] = $array;
            $offData['name'] = array_column($array,'name');
        }
        
        return ResultVOUtil::success($offData);
    }
    
    public function repair(){
        $agency_id = session('admin.agency_id');
        $data = [];
        if (strpos($agency_id, '1') === false) {
            $data = Repair::where('agency_id', 'in', $agency_id)->where('shift_time','eq',0)->order('in_time asc')->limit(5)->select();
        } else {
            $data = Repair::where('shift_time','eq',0)->order('in_time asc')->limit(5)->select();
        } 
        return ResultVOUtil::success($data);
    }
    
    public function usage(){
       $agency_id = session('admin.agency_id');
        
        if (strpos($agency_id, '1') === false) {
           $agencyModel = Agency::withCount(['cars'])->select($agency_id);
        } else{
           $agency = Agency::where('id','>',1)->column('id');
           $agencyModel = Agency::withCount(['cars'])->select($agency);
        }
        $agencyData = [];
        foreach ($agencyModel as $key => $value) {
            $agencyData[$key]['name'] = $value->name;
            $agencyData[$key]['value'] = $value->cars_count;
            $agencyData[$key]['id'] = $value->id;
        }
        $sortData = $this->array_sort($agencyData,'value',SORT_DESC);
        $agencyName = array_column($sortData, 'name');
        $sortId = array_column($sortData, 'id');
        $type= 0;
        $array = [];
        $j = 1;
        $car_usage = ['未知','库存','分时租赁','长租','以租代购','企业用车','共享用车','公务用车','自测用车','销售'];
        for ($i=0; $i < 9; $i++) {
            $model = Agency::withCount(['cars'=>function($query){
                global $type;$type++;
                $query->where('car_usage',$type);
            }])->select($sortId);
            $cars = [];
            foreach ($model as $value) {
                $key=0;
                $key = array_search($value->id,$sortId);
                $cars[$key] = $value->cars_count;
            }
            ksort($cars);
            $array[] = [
                'name'=>$car_usage[$j],
                'type'=> 'bar',
                'stack'=> '总量',
                'data'=>$cars
            ];
            $j++;
            
        }
        return ResultVOUtil::success(['agency'=>$agencyName,'data'=>$array]);
    }
    
    public function count(){
        $data = [];
        $data['cars'] = Cars::count();
        
        $car_unique_id = Cars::column('car_unique_id');
        $data['online'] = CarsInfo::where('car_unique_id','in',$car_unique_id)->where(['network_status'=>1])->count();
        
        $agency_id = session('admin.agency_id'); 
        if (strpos($agency_id, '1') === false) {
            $data['abnormal']= OffAbnormal::where('agency_id','in',$agency_id)->count();
            $data['normal'] = OffNormal::where('agency_id','in',$agency_id)->count();
        } else{
            $data['abnormal'] =OffAbnormal::count();
            $data['normal'] = OffNormal::count();
        }
        $data['offline'] = $data['abnormal'] + $data['normal'];
        
        $data['machine'] = $data['cars'] - $data['online'] - $data['offline'];
        return ResultVOUtil::success($data);
    }
    
    public function location(){
        $agency_id = session('admin.agency_id');
        if (strpos($agency_id, '1') === false) {
            
            $data = Db::query("SELECT a.car_sn,b.longitude,b.latitude FROM `joy_cars` a INNER JOIN `joy_cars_info`  b on a.car_unique_id = b.car_unique_id WHERE (`delete_time` IS NULL  AND a.agency_id in (?) AND b.network_status = 1)",[$agency_id]);
        } else{
            $data = Db::query("SELECT a.car_sn,b.longitude,b.latitude FROM `joy_cars` a INNER JOIN `joy_cars_info`  b on a.car_unique_id = b.car_unique_id WHERE (`delete_time` IS NULL  AND b.network_status = 1)");
        }
        return ResultVOUtil::success($data);
    }
    
    public function array_sort($array, $on, $order=SORT_ASC)
    {
        $new_array = array();
        $sortable_array = array();
        
        if (count($array) > 0) {
            foreach ($array as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        if ($k2 == $on) {
                            $sortable_array[$k] = $v2;
                        }
                    }
                } else {
                    $sortable_array[$k] = $v;
                }
            }
            
            switch ($order) {
                case SORT_ASC:
                    asort($sortable_array);
                    break;
                case SORT_DESC:
                    arsort($sortable_array);
                    break;
            }
            
            foreach ($sortable_array as $k => $v) {
                array_push($new_array, $array[$k]);
            }
        }
        
        return $new_array;
    }
}
